<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/8/9
 * Time: 14:08
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\validate;


use think\Validate;

class Goods extends Validate
{
    /**
     * @var string[]
     */
    protected $rule = [
        "title" => "require|chsDash",
        "category_id" => "require|strExp",
        "promotion_title" => "require|chs",
        "keywords" => "require",
        "sub_title" => "require",
        "goods_specs_type" => "require|integer|in:1,2",
        "skus" => "requireIf:goods_specs_type,2",
        "price" => "requireIf:goods_specs_type,1",
        "cost_price" => "requireIf:goods_specs_type,1",
        "production_time" => "require",
        "big_img" => "require|max:255",
        "recommend_image" => "require|max:255",
        "images" => "require|array",
        "is_show_stock" => "in:1",
        "stock"=>"requireIf:goods_specs_type,1"
    ];
    /**
     * @var string[]
     */
    protected $message = [
        "title.require" => "商品名不能为空",
        "title.chsDash" => "商品名称格式只能是汉字、字母、数字和下划线_及破折号-",
        "category_id.require" => "商品分类不能为空",
        "promotion_title.require" => "商品单位不能为空",
        "promotion_title.chs" => "商品单位只能为汉字",
        "keywords.require" => "关键词不能为空",
        "sub_title.require" => "副标题不能为空",
        "is_show_stock" => "",
        "goods_specs_type.require" => "商品规则不能为空",
        "goods_specs_type.integer" => "商品规则类型错误",
        "goods_specs_type.in" => "商品规则类型错误",
        "skus.requireIf" => "请选择规则",
        "price.requireIf" => "现价不能为空",
        "cost_price.requireIf" => "原价不能为空",
        "production_time.require" => "生产日期不能为空",
        "big_img.require" => "请上传大图",
        "big_img.max" => "大图文件内部错误！请重新上传",
        "recommend_image.require" => "请上传推荐图",
        "recommend_image.max" => "推荐图文件内部错误！请重新上传",
        "images.require" => "请上传轮播图",
        "images.array" => "轮播图类型错误！请删除重新上传",
        "is_show_stock.in" => "显示库存类型错误",
        "stock.requireIf"=>"请输入库存"
    ];
    /**
     * @var array
     */
    protected $scene = [
        "add" => ['title', 'category_id', 'promotion_title', 'keywords', 'sub_title', 'goods_specs_type', 'skus', 'price', 'const_price', 'production_time', 'big_img', 'recommend_image', 'images', "is_show_stock","stock"]
    ];

    /**
     * 验证商品分类是否选择三级
     * @param $value
     * @param $data
     * @return bool|string
     */
    function strExp($value, $data)
    {
        if (empty($value)) {
            return "必须选择商品分类";
        }
        list($str1, $str2, $str3) = explode(',', $value);

        if ($str1 == "undefined" || $str2 == "undefined" || $str3 == "undefined") {
            return "商品分类选择不完整";
        }
        return true;
    }
}